﻿using DefaultDspPlugins.UI.Common.Modulizer.Engine.GUI;
using DefaultDspPlugins.UI.Common.Modulizer.Engine.GUIv2.Editors;
using DefaultDspPlugins.UI.Common.Modulizer.Engine.GUIv2.Sequencer;
using SoundEngine3.SequenceContent;
using System.Collections.Generic;
using System.Linq;
using ZquenceStudio3Host.DspPlugins.Synthesizers.Nodesizer;
using ZquenceStudio3Host.DspPlugins.Synthesizers.Nodesizer.Engine;
using ZquenceStudio3Host.DspPlugins.Synthesizers.Nodesizer.Engine.GUI;
using ZquenceStudio3Host.DspPlugins.Synthesizers.Nodesizer.Engine.GUI.Controls;

namespace ZquenceStudio3Host.Commands.BuiltInCommands
{
    internal class TransformModuleCommandV1 : BaseTransformCommand<ModulePanel, BasicSequenceState>
    {
        private List<BaseModule> mModules;
        private BaseModulizerUI mNodisizer;
        private ModulizerUIv2 mNodisizer1;

        public override object[] ReferenceObjects
        {
            get { return null; }
        }

        public TransformModuleCommandV1(IZquenceStudio3HostApplication application, BaseModulizerUI nodisizer, TrackArea.IBaseAreaInterface baseArea, IEnumerable<ModulePanel> modulePanels)
            : base(application, baseArea, modulePanels)
        {
            NoIndexSnap = true;

            mNodisizer = nodisizer;

            mModules = new List<BaseModule>(modulePanels.Count());
            foreach (var modulePanel in modulePanels)
            {
                mModules.Add(modulePanel.Module);
            }
        }
        
        protected override void Execute()
        {
            base.Execute();

            UpdatePosition();
        }

        protected override void UnExecute()
        {
            base.UnExecute();

            UpdatePosition();
        }

        private void UpdatePosition()
        {
            RebuildGraphicsIfNessesary();
        }

        private void RebuildGraphicsIfNessesary()
        {

            // Only update if we are not inside moving modules parent.
            if (mNodisizer.CurrentModuleContainer != mModules[0].ParentModule)
            {
                mNodisizer.RebuildSynthGraphModuleParent(mModules[0]);
            }
        }

        public override string ToString()
        {
            return "Transformed module.";
        }
    }

    internal class TransformModuleCommand : BaseTransformCommand<ITransformSequenceObject, BasicSequenceState>
    {
        private List<BaseModule> mModules;
        private BaseModulizerUI mNodisizer;
        private ModulizerUIv2 mNodisizer1;

        public override object[] ReferenceObjects
        {
            get { return null; }
        }

        public TransformModuleCommand(IZquenceStudio3HostApplication application, BaseModulizerUI nodisizer, TrackArea.IBaseAreaInterface baseArea, IEnumerable<ModulePanel> modulePanels)
            : base(application, baseArea, modulePanels)
        {
            NoIndexSnap = true;
            mNodisizer = nodisizer;

            mModules = new List<BaseModule>(modulePanels.Count());
            foreach (var modulePanel in modulePanels)
            {
                mModules.Add(modulePanel.Module);
            }
        }

        public TransformModuleCommand(IZquenceStudio3HostApplication application, ModulizerUIv2 modisizer1, ModulizerTrackArea baseArea, IEnumerable<ITransformSequenceObject> modulePanels)
            : base(application, baseArea, modulePanels)
        {
            NoIndexSnap = true;
            mNodisizer1 = modisizer1;
            mModules = new List<BaseModule>(modulePanels.Count());
            foreach (TrackModulePanel modulePanel in modulePanels)
            {
                mModules.Add(modulePanel.Module);
            }
        }

        protected override void Execute()
        {
            base.Execute();

            UpdatePosition();
        }

        protected override void UnExecute()
        {
            base.UnExecute();

            UpdatePosition();
        }

        private void UpdatePosition()
        {
            RebuildGraphicsIfNessesary();
        }

        private void RebuildGraphicsIfNessesary()
        {
            if (mNodisizer1 != null)
            {
                // Only update if we are not inside moving modules parent.
                if (mNodisizer1.CurrentModuleContainer != mModules[0].ParentModule)
                {
                    mNodisizer1.GoInto(mModules[0]);
                }
            }
            else
            {
                // Only update if we are not inside moving modules parent.
                if (mNodisizer.CurrentModuleContainer != mModules[0].ParentModule)
                {
                    mNodisizer.RebuildSynthGraphModuleParent(mModules[0]);
                }
            }
        }

        public override string ToString()
        {
            return "Transformed module.";
        }
    }
}
